package com.financing.platform.task;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.mail.dto.MailContentDTO;
import com.financing.mail.service.MailSenderService;
import com.financing.platform.dao.IMessageMailDao;
import com.financing.platform.dao.ISwitchDao;
import com.financing.platform.dto.MessageMailDTO;
import com.financing.platform.dto.SwitchDTO;
import com.financing.platform.util.Dict;

/**
 * 
* @ClassName: MailSendJob 
* @Description: 系统邮件下发
* @author chenming 
* @date 2014年9月22日 下午6:32:23 
*
 */
@Component(value="mailSendJob" )
public class MailSendJob {
	
	Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private IMessageMailDao messageMailDaoImpl;
	
	@Autowired
	private ISwitchDao switchDaoImpl;
	
	@Resource
	private MailSenderService mailSenderService;
	
	private static boolean runTimeFlag = false;
	
//	@Scheduled(cron = "0 0/2 * * * ?") 
	public void sendMessageJob(){
		
		if(runTimeFlag){
			return ;
		}else{
			runTimeFlag = true;
		}
		
		MessageMailDTO messageMailDTO  = new MessageMailDTO();
		log.info("begin send mail message job....");
				
		messageMailDTO.setFpMailSendTime(null);
		messageMailDTO.setFpMailSendType(Dict.message_type_now);
		messageMailDTO.setFpMailSendStatus(Dict.message_status_not_send);
		messageMailDTO.setFpMailSendTimes(Dict.message_max_send_times);
		messageMailDTO.setBegin(0);
		messageMailDTO.setEnd(200);
		messageMailDTO.setUpdateBy("system");
		messageMailDTO.setUpdateDate(new Date());
				//实时发送的邮件
		for(int i = 0;i<=3;i++){
			List<MessageMailDTO> messageList = messageMailDaoImpl.messageMailList(messageMailDTO);
			for(MessageMailDTO message:messageList){
				try {
					messageMailDaoImpl.updateMessageMailStatus(message);
					messageSend(message);
				} catch (SQLException e) {
					log.warn("update Message status error:",e);
				}
			}
		}
							
		//定时发送的邮件
		messageMailDTO.setFpMailType(Dict.message_type_after);
		messageMailDTO.setFpMailSendTime(new Date());
		List<MessageMailDTO> messageList = messageMailDaoImpl.messageMailList(messageMailDTO);
		for(MessageMailDTO message:messageList){
			message.setFpMailSendStatus(Dict.message_send_status_sending);
			try {
				messageMailDaoImpl.updateMessageMailStatus(message);
				messageSend(message);
			} catch (SQLException e) {
				log.warn("update Message status error:",e);
			}
		}
		runTimeFlag = false;
	log.info("end send mail message job....");
	}

	/**
	 * 
	* @Title: messageSend 
	* @Description:  发送短信息
	* @param   message    
	* @return void   
	* @throws
	 */
private void messageSend(MessageMailDTO message) {
	
			//创建邮件
			MailContentDTO mail = new MailContentDTO();
			mail.setRecAddress(message.getFpMailTo());
			mail.setSubject(message.getFpMailTitle());
			mail.setContext(message.getFpMailContent());
			
			boolean sendResult = true;
			
			SwitchDTO switchs = new SwitchDTO();
			switchs.setsKey(message.getFpMailType());
			switchs = switchDaoImpl.querySwitch(switchs);
			if(null != switchs && Dict.switchOn.equals(switchs.getsValue())){
				try{
					sendResult = mailSenderService.send(mail);
				}catch(Exception e){
					log.warn("send mail  error!", e);
					sendResult = false;
				}
			}
			if(sendResult){
				message.setFpMailSendStatus(Dict.message_send_status_success);
				message.setFpMailSendTime(new Date());
			}else{
				message.setFpMailSendStatus(Dict.message_send_status_fail);
				message.setFpMailSendTime(new Date());
			}
			
			try {
				message.setFpMailSendTimes(message.getFpMailSendTimes()==null?0:message.getFpMailSendTimes()+1);
				message.setUpdateDate(new Date());
				messageMailDaoImpl.updateMessageMailStatus(message);
			} catch (SQLException e) {
				log.warn("update message mail error!",e);
			}
		}
}
